<?php 
namespace App\Models;
use Log;
use App\Cache\BaseCache;

class Attention extends BaseModel{
    
	//表明
	protected $table = 'attention';
	//主键
	protected $primaryKey = 'id';
	
    public function addAttention(){
        $type = $this->request->input('type');
        $userId = $this->request->input('userId');
        $uid = $this->request->input('uid');
        $attention = $this->select('id','type','follower_id','fans_id')
                ->where('follower_id','=',$uid)
                ->where('fans_id','=',$userId)
                ->where('type','=',$type)
                ->where('is_delete','=',0)
                ->first();
        if($attention){
            return TRUE;
        }
        $attention = new Attention($this->request);
        $attention->follower_id = $uid;
        $attention->fans_id = $userId;
        $attention->type = $type;
        $attention->save();
        return TRUE;
    }
    
    public function delAttention(){
        $type = $this->request->input('type');
        $userId = $this->request->input('userId');
        $uid = $this->request->input('uid');
        $attention = $this->select('id','type','follower_id','fans_id')
                ->where('follower_id','=',$uid)
                ->where('fans_id','=',$userId)
                ->where('type','=',$type)
                ->where('is_delete','=',0)
                ->first();
        if($attention){
            $attention->is_delete = 1;
            $attention->save();
            return TRUE;
        }
        return TRUE;
    }
    
    //判断是否预定
    public function isAttention($type,$uid,$userId){
        $attention = $this->select('id','type','follower_id','fans_id')
                ->where('follower_id','=',$uid)
                ->where('fans_id','=',$userId)
                ->where('type','=',$type)
                ->where('is_delete','=',0)
                ->first();
        if(!$attention){
           $attention = new Attention($this->request);
           $attention->follower_id = $uid;
           $attention->fans_id = $userId;
           $attention->type = $type;
           $attention->is_delete = 1;
        }
        $attention->saveModel();
        return !$attention->is_delete ;
    }
    
    //更新实体
    public function saveModel(){
        $cache = new BaseCache();
        $cache->method = 'forever';
        $cache->cacheName = 'attention';
        $cache->cachePar = array($this->type,$this->follower_id,$this->fans_id); //集合
        if($this->is_delete == 0){
            $cache->cacheValue = 'ATTENTION';
        } else {
            $cache->method = 'put';
            $cache->cacheValue = 'UNATTENTION';
        }
        $cache->HandleCache();
    }
    
    //删除实体
    public function deleteModel(){
        $cache = new BaseCache();
        $cache->method = 'forever';
        $cache->cacheName = 'attention';
        $cache->cachePar = array($this->type,$this->follower_id,$this->fans_id); //集合
        $cache->cacheValue = 'UNATTENTION';
        $cache->HandleCache();
    }
    
}